#!/bin/bash

# Run Python visualization script
# This script will run the pcd_web.py visualization tool

set -e # Exit on any error

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Project root directory (parent of scripts directory)
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
PYTHON_SCRIPT="$PROJECT_DIR/tools/pcd_web.py"

echo -e "${BLUE}=== Running PCD Web Visualization ===${NC}"

# Check if Python script exists
if [ ! -f "$PYTHON_SCRIPT" ]; then
    echo -e "${RED}Error: Python script not found at $PYTHON_SCRIPT${NC}"
    exit 1
fi

# Check if Python 3 is available
if ! command -v python3 &>/dev/null; then
    echo -e "${RED}Error: Python 3 is not installed or not in PATH${NC}"
    exit 1
fi

# Check if required Python packages are available
echo -e "${YELLOW}Checking Python dependencies...${NC}"

# Function to check if a Python package is installed
check_package() {
    python3 -c "import $1" 2>/dev/null
    if [ $? -ne 0 ]; then
        echo -e "${RED}Error: Python package '$1' is not installed${NC}"
        echo -e "${YELLOW}Install it with: pip3 install $1${NC}"
        return 1
    fi
}

# Check required packages
MISSING_PACKAGES=0

if ! check_package "open3d"; then
    MISSING_PACKAGES=1
fi

if ! check_package "plotly"; then
    MISSING_PACKAGES=1
fi

if ! check_package "numpy"; then
    MISSING_PACKAGES=1
fi

if [ $MISSING_PACKAGES -eq 1 ]; then
    echo -e "${YELLOW}To install all required packages, run:${NC}"
    echo -e "${GREEN}pip3 install open3d plotly numpy${NC}"
    exit 1
fi

echo -e "${GREEN}✓ All dependencies found${NC}"

# Change to project directory
cd "$PROJECT_DIR"

# Check if PCD files exist in various locations
PCD_FILE=""
if [ -f "$PROJECT_DIR/test_data/map_only23456.pcd" ]; then
    PCD_FILE="$PROJECT_DIR/test_data/map_only23456.pcd"
elif find "$PROJECT_DIR" -name "*.pcd" -type f | head -1 >/dev/null 2>&1; then
    PCD_FILE=$(find "$PROJECT_DIR" -name "*.pcd" -type f | head -1)
    echo -e "${YELLOW}Using PCD file: $PCD_FILE${NC}"
else
    echo -e "${YELLOW}Warning: No PCD files found in the project${NC}"
    echo -e "${YELLOW}The tool will search for PCD files automatically${NC}"
fi

echo -e "${GREEN}Starting PCD web visualization...${NC}"

# Run the Python script with optional PCD file argument
if [ -n "$PCD_FILE" ]; then
    python3 "$PYTHON_SCRIPT" "$PCD_FILE"
else
    python3 "$PYTHON_SCRIPT"
fi
EXIT_CODE=$?

echo ""
if [ $EXIT_CODE -eq 0 ]; then
    echo -e "${GREEN}Web visualization completed successfully${NC}"
    echo -e "${GREEN}Check the 'web_visualization' directory for output files${NC}"

    # Check if web files were created
    if [ -f "$PROJECT_DIR/web_visualization/index.html" ]; then
        echo -e "${BLUE}Open the following file in your browser:${NC}"
        echo -e "${GREEN}file://$PROJECT_DIR/web_visualization/index.html${NC}"
    fi
else
    echo -e "${RED}Web visualization failed with exit code: $EXIT_CODE${NC}"
fi

echo -e "${BLUE}=== Web Visualization Complete ===${NC}"
exit $EXIT_CODE
